package com.ep.thirdPlatformProject.job.service.impl;

import com.ep.thirdPlatformProject.erp.entity.vo.ErpBudgetResultVo;
import com.ep.thirdPlatformProject.erp.service.ErpBudgetService;
import com.ep.thirdPlatformProject.job.service.ErpToOAService;
import com.ep.thirdPlatformProject.oa.service.OAApiService;
import com.ep.thirdPlatformProject.system.util.MailUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

@Service
@Slf4j
@RequiredArgsConstructor
public class ErpToOAServiceImpl implements ErpToOAService {
    private final ErpBudgetService erpBudgetService;

    private final MailUtil mailUtil;

    private final OAApiService oaApiService;

    @Override
    public String syncBudgetForm(String[] deptIds) {
//        MPJLambdaWrapper<ErpBudgetMainEntity> queryWrapper = new MPJLambdaWrapper<ErpBudgetMainEntity>()
//                .selectAs(ErpBudgetMainEntity::getBudgetCompanyCode, ErpBudgetResultVo::getBudgetCompanyCode)
//                .selectAs(ErpBudgetMainEntity::getBudgetManagerCode, ErpBudgetResultVo::getBudgetManagerCode)
//                .selectAs(ErpBudgetMainEntity::getBudgetDeptId, ErpBudgetResultVo::getBudgetDeptId)
//                .selectAs(ErpBudgetMainEntity::getBillNo, ErpBudgetResultVo::getBillNo)
//                .selectAs(ErpBudgetMainEntity::getEnterpriseId, ErpBudgetResultVo::getEnterpriseId)
//                .selectAs(ErpBudgetMainEntity::getPeriods, ErpBudgetResultVo::getConsecutivePeriod)
//                .selectCollection(ErpBudgetDetailEntity.class, ErpBudgetResultVo::getDetailList)
//                .leftJoin(ErpBudgetDetailEntity.class, on ->
//                        on.eq(ErpBudgetMainEntity::getBillNo, ErpBudgetDetailEntity::getBillNo)
//                                .eq(ErpBudgetMainEntity::getEnterpriseId, ErpBudgetDetailEntity::getEnterpriseId)
//
//                )
//                .eq(ErpBudgetMainEntity::getStatusCode, "Y")
//                .eq(ErpBudgetMainEntity::getVersionId, 1);
//        if (deptIds != null && deptIds.length > 0) {
//            queryWrapper.in(ErpBudgetMainEntity::getBudgetDeptId, Arrays.stream(deptIds).toList());
//        }
        List<ErpBudgetResultVo> allListData = erpBudgetService.getAllListData(deptIds);

        String failedMessage = "预算编制单生成失败！";
        return constructResultStr(allListData, oaApiService::constructBudgetFormWorkFlow, failedMessage);
    }


    /**
     * 构造结果STR
     *
     * @param list     列表
     * @param function 函数
     * @param failMsg  失败味精
     * @return {@link String }
     */
    private String constructResultStr(List<ErpBudgetResultVo> list, Function<ErpBudgetResultVo, String> function, String failMsg) {

        // 拟定最终结果集
        List<String> successList = new ArrayList<>();
        List<String> failList = new ArrayList<>();

        String successStr = "";
        String failStr = "";

        for (ErpBudgetResultVo t : list) {
            // 执行上述func，返回流程结果
            String result = function.apply(t);
            StringBuilder sb = new StringBuilder();
            sb.append(t.getBudgetManagerCode())
                    .append(">>>")
                    .append(t.getBudgetManagerCode())
                    .append(":")
                    .append(t.getBudgetDeptId())
                    .append(":")
                    .append("对象单号为：\n")
                    .append(t.getBillNo())
                    .append("\n");
            // 如果结果为流程编号，则为成功
            if (result.matches("-?\\d+(\\.\\d+)?")) {
                sb.append("成功，返回流程编号为：")
                        .append(result);
                // 下推erp的结果
                String message = erpBudgetService.postToErpAndChangeStatus(t);
                sb.append("\n").append(message);
                successList.add(sb.toString());
            } else {
                sb.append("失败了，原因为：").append(result);
                failList.add(sb.toString());
            }
        }
        if (!successList.isEmpty()) {
            successStr = "成功的如下：" + String.join("\n", successList);
        }
        if (!failList.isEmpty()) {
            failStr = "失败的如下：" + "\n\n\n" + String.join("\n", failList);
            mailUtil.sendSimpleMail(failMsg, failStr);
        }
        return successStr + "\n\n\n----------------------------------------\n\n\n" + failStr;
    }
}
